package controller

import (
	"cooke-home/common/model/base_dto"
	"cooke-home/common/response"
	"cooke-home/stock-service/model/dto"
	"cooke-home/stock-service/service"

	"github.com/gin-gonic/gin"

	"strconv"
)

type StockController struct {
	stockService service.StockService
}

func NewStockController(stockService service.StockService) *StockController {
	return &StockController{
		stockService: stockService,
	}
}

func (tc *StockController) Save(c *gin.Context) {
	var req dto.StockDto

	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}

	if req.StockName == "" {
		response.Error(c, "stock_name is required", nil)
		return
	}

	if req.Content == "" {
		response.Error(c, "content is required", nil)
		return
	}

	err := tc.stockService.Save(&req)

	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}

	response.Success(c, "success", nil)
}

func (tc *StockController) Delete(c *gin.Context) {
	var req base_dto.BatchDeleteDto

	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}

	err := tc.stockService.Delete(req)

	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}

	response.Success(c, "success", nil)
}

func (tc *StockController) Update(c *gin.Context) {
	var req dto.StockDto

	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}

	if req.StockID == 0 {
		response.Error(c, "id is required", nil)
		return
	}

	err := tc.stockService.Update(&req)

	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}

	response.Success(c, "success", nil)
}

func (tc *StockController) Get(c *gin.Context) {
	idStr, ok := c.GetQuery("id")
	if !ok || idStr == "" || idStr == "0" {
		response.Error(c, "id is required", nil)
		return
	}

	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		response.Error(c, "invalid id format", nil)
		return
	}

	stock, err := tc.stockService.Get(id)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}

	response.Success(c, "success", stock)
}

func (tc *StockController) GetList(c *gin.Context) {
	idStr, ok := c.GetQuery("parent_id")
	if !ok || idStr == "" {
		response.Error(c, "id is required", nil)
		return
	}

	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		response.Error(c, "invalid id format", nil)
		return
	}

	stocks, err := tc.stockService.GetList(id)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}

	response.Success(c, "success", stocks)
}

func (tc *StockController) Clear(c *gin.Context) {
	var req dto.StockDto
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}

	if req.StockID == 0 {
		response.Error(c, "id is required", nil)
		return
	}

	err := tc.stockService.Clear(&req)

	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "success", nil)
}

func (tc *StockController) Minus(c *gin.Context) {
	var req dto.StockDto
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}

	if req.StockID == 0 {
		response.Error(c, "id is required", nil)
		return
	}

	if req.number == 0 {
		response.Error(c, "number is required", nil)
		return
	}

	err := tc.stockService.Minus(&req)

	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "success", nil)
}
